/*Take the manifesto data and */  
/*Define the variables rvar, salienzmean, rmax*/



	/*Drop Election before 1975*/
	drop if edate <mdy(1,1,1975)


	/*Generate Variable for XR issues*/
	gen right=.
	replace right=per109-per107+per601-per602+per605+per608-per607

	/*Salience of XR issues*/
	gen salienzright=.
	replace salienzright=per109+per107+per601+per602+per605+per608+per607
	
	/*Drop XR parites*/
	drop if party == 42420 /*FP�*/
	drop if party == 21914 /*VB*/
	drop if party == 13951 /*D�n. Fortschrittspartei*/
	drop if party == 13720 /*D�nische Volkspartei */
	drop if party == 14820 /*Finnland SMP*/
	drop if party == 31720 /*FN FR*/
	drop if party == 34512 /*POLA*/
	drop if party == 34610 /*EPEN */
	drop if party == 32710 /*MSI etc.*/
	drop if party == 32720 /*Lega Nord*/
	drop if party == 22720 /* LPF*/
	drop if party == 12951 /*Norw. Progressparty*/
	drop if party == 11951 /*Ny Demokrati*/
	
	/*Waloon Rally */
	drop if party==21911

	
	/*Maximum f�r each election*/
	egen rmax=max(right),by(sortcountry edate)


	/*Preisfrage: Wie bekommen wir rmax f�r die n�chstliegende Wahl an die Mikro-Daten kumuliert?*/


  /*Interpolate party positions between elections*/


	drop rmax
	keep edate party sortcountry right salienzright pervote
	
	save fabzwischen,replace

	forvalues land=1/15 {
		use fabzwischen,replace
		append using befragungsdaten
		keep if sortc==`land'

		

                  /*One variable per party*/
                  
                qui separate right,by(party)
		/*Average*/
		qui separate pervote,by(party)

		/*Salience*/
		qui separate salienzright,by(party)
		
		/*Number of parties in each country?*/
		qui levelsof(party)
		local hilfe=r(levels)
		local parteienzahl : list sizeof hilfe
		
		forvalues v=1/`parteienzahl' {
			ipolate right`v' edate,gen(iright`v')
			}

		/*interpolate salience*/
		forvalues v=1/`parteienzahl' {
			ipolate salienzright`v' edate,gen(isalienzright`v')
			}
		
		
		/*vote shares*/
		forvalues v=1/`parteienzahl' {
			ipolate pervote`v' edate,gen(ipervote`v')
			}
		
		/*find maximum*/
		local maxstring "iright1"

		forvalues v=2/`parteienzahl' {
			local maxstring "`maxstring', iright`v'"
			}
		display "`maxstring'"
		gen imax=max(`maxstring')

		/*weighted average*/
		forvalues v=1/`parteienzahl' {
			gen teilsumme`v'=ipervote`v'*iright`v'
			}

		/*weighted average: salience*/
		forvalues v=1/`parteienzahl' {
			gen salienzteilsumme`v'=ipervote`v'*isalienzright`v'
			}
		
		
		egen teiler=rowtotal(ipervote1-ipervote`parteienzahl')
		egen rmean=rowtotal(teilsumme1-teilsumme`parteienzahl')
		replace rmean=rmean/teiler

		/*average salience*/
		egen salienzmean=rowtotal(salienzteilsumme1-salienzteilsumme`parteienzahl')
		replace salienzmean=salienzmean/teiler
		
		/*variance*/

		/*squared deviations*/
		forvalues v=1/`parteienzahl' {
			gen gabwq`v'=((iright`v'-rmean)^2)*ipervote`v'
			}
		egen saq=rowtotal(gabwq1-gabwq`parteienzahl')
		replace saq=saq/teiler
		ren saq rvar
		
		
		keep edate sortc imax rmean rvar salienzmean kontext 
		ren imax rmax
		lab var rmax "Maximum position (interpolated)"
		lab var rmean "Average position (weighted, interpolated)"
		sort edate

                /*Some EBs were collected after the last election held in a given country. In this case, the missing value of rmax is replaced*/
                  /*by its last known value (i.e. the value for the previous election*/

		replace rmax=rmax[_n-1] if rmax==.
		replace rmean=rmean[_n-1] if rmean==.
		replace salienzmean=salienzmean[_n-1] if salienzmean==.
		replace rvar=rvar[_n-1] if rvar==.
		/*Just for fun: logged variance*/
		gen logrvar=log(rvar)
		keep if kontext ~=.
		/*save (per country)*/
		save rmax-land-`land',replace
		}
	
	/*Combine per-country datasets*/

	use rmax-land-1,replace
	forvalues land=1/15 {
		append using rmax-land-`land'
		}

	sort kontext
	save rmax,replace


